package com.wangchao523.util.servletUtil;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * action url访问的解析servlet,
 * @author corey
 */
@SuppressWarnings("serial")
public class ActionServlet extends HttpServlet {
	
	Logger log = Logger.getLogger(this.getClass().getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		String clientUrl = req.getRequestURI();	//前端访问的url地址,不含？后的参数
		log.info("访问地址：" + clientUrl);
		
		String className = clientUrl.substring(clientUrl.lastIndexOf("/")+1);
		className = className.substring(0, className.lastIndexOf("."));
		log.info("访问ACTION：" + className);
		
		String methodName = req.getParameter("m");
		if(methodName == null || "".equals(methodName)){
			to404(resp, clientUrl);
		}else{
			Object action = this.getClass(className);
			String result = (String) this.doMethod(req, resp, action, methodName);
			RequestDispatcher dispatcher = req.getRequestDispatcher(this.getViewPath(className, result));//返回展示层
			try {
				dispatcher.forward(req, resp);
			} catch (Exception e) {
				log.error("跳转资源出错：" + e.toString());
				toError(resp);
			}
		}
	}
	
	/**
	 * 根据名字获取action的类
	 * @param className
	 * @return
	 */
	private Object getClass(String className){
		Object o = null;
			try {
				String pkgClassName = InitServlet.actionProperties.getProperty(className);
				o = Class.forName(pkgClassName).newInstance();
			} catch (Exception e) {
				log.error("获取class出错" + e.toString());
			}
		return o;
	}
	
	/**
	 * 执行method的方法
	 * @param action
	 * @param req
	 * @param resp
	 * @return
	 */
	private Object doMethod(HttpServletRequest req, HttpServletResponse resp, Object action, String methodName){
		Object o = null;
		try {
			Method doMethod = action.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			o = doMethod.invoke(action, req, resp);
		} catch (Exception e) {
			log.error("动态代理执行方法出错：" + e.toString());
		}
		return o;
	}
	
	/**
	 * 根据类名和返回的stirng判断前台页面的路径
	 * @param result
	 * @return
	 */
	private String getViewPath(String className, String result){
		String path = "";
		if(result == null || "".equals(result)){
			path = "/error.html";
		}else{
			path = "/" + InitServlet.vmPath + "/" + className + "/" + result + ".vm";
		}
		return path;
	}

	/**
	 * 404页面
	 * @param resp
	 * @param clientUrl
	 */
	private void to404(HttpServletResponse resp, String clientUrl){
		log.warn("该地址访问不存在：" + clientUrl);
		try {
			resp.sendRedirect("/404.html");
		} catch (IOException e) {
			log.error("404页面文件没找到");
		}
	}
	/**
	 * error页面
	 * @param resp
	 */
	private void toError(HttpServletResponse resp){
		try {
			resp.sendRedirect("/error.html");
		} catch (IOException e1) {
			log.error("error页面文件没找到");
		}
	}
}
